package cn.abcsys.cloud.devops.web.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.web.entity.ClusterEnv;
import cn.abcsys.cloud.devops.web.entity.Env;
import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.entity.User;
import cn.abcsys.cloud.devops.web.service.ClusterEnvService;
import cn.abcsys.cloud.devops.web.service.EnvUserService;
import cn.abcsys.cloud.devops.web.util.Result;

/**
 * 环境控制层
 * @author xzg
 *
 */
@Controller
@RequestMapping("/env")
public class EnvUserController {
	
	private static final Logger LOGGER = Logger.getLogger(EnvUserController.class);
	
	@Resource
	private EnvUserService envUserService;
	
	@Resource
	private ClusterEnvService clusterEnvService;
	/**
	 * 查询角色信息
	 * @param listURE
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public GridBean selectEnvByuser(@RequestBody Map<String, Object> params){
		//List<Role> roleList = roleService.getRoleByName(role);
		if( null == params){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			try{
				//JSONObject paramIn = JSONObject.parseObject(params);
				JSONObject paramIn = new JSONObject(params);
				PagerModel pagerModel = paramIn.toJavaObject(PagerModel.class);
				Env env = new Env();
				env.setEnvName(paramIn.getString("envName"));
				return envUserService.getEnvByName(env,pagerModel);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return null;
			}
		}
	}
	/**
	 * 修改环境信息
	 * @param request
	 * @param envId
	 * @return
	 */
	@RequestMapping(value = "/updateEnv", method = { RequestMethod.POST})
	@ResponseBody
	public Result updateEnv(HttpServletRequest request,@RequestBody Env env){
		if(null == env)
			return new Result(false, "没有数据");
		try {
			 envUserService.updateEnvById(env);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("数据库更新失败");
			return new Result(false, "环境信息更新失败");
		}
		return new Result(true, "环境信息更新成功");
	}
	
	/**
	 * 环境信息删除
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/delete",method={RequestMethod.POST})
	@ResponseBody
	public Result deleteEnvById(@RequestBody Map<String,Integer> map){
		
		Integer envId = map.get("envId");
		if(null == envId)
			return new Result(false, "无请求数据");
		try {
			envUserService.deleteEnvById(envId);
			return new Result(true, "环境删除成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("环境删除失败");
			return new Result(false, "删除环境信息失败");
		}
	}
	
	/**
	 * 环境信息新增
	 * @param env
	 * @return
	 */
	@RequestMapping(value="/createEnv",method={RequestMethod.POST})
	@ResponseBody
	public Result createENv(HttpServletRequest request,@RequestBody Map<String,Object> map){
		try {
			User loginUser = (User)request.getSession().getAttribute("user");
			if(null == loginUser)
				return new Result(false, "会话失效");
			Env env = new Env();
			env.setEnvName((String)map.get("envName"));
			env.setEnvDesc((String)map.get("envDesc"));
			env.setEnvCreator(loginUser.getUserId());
			 envUserService.createEnv(env);
			 //插入环境集群表env.getEnvId()
			 ClusterEnv clusterEnv = new ClusterEnv();
			 clusterEnv.setClusterServiceIp((String)map.get("clusterServiceIp"));
			 clusterEnv.setEnvId(env.getEnvId());
			 clusterEnv.setClusterId((Integer)map.get("clusterId"));;
			 clusterEnvService.createCLusterEnv(clusterEnv);
			//LOGGER.info("插入环境Id："+env.getEnvId());
			return new Result(true, "环境新增成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("新增环境失败");
			return new Result(false, "环境新增失败");
		}
	}
	
	/**
	 * 超级管理员获取所有的环境信息
	 * @return
	 */
	@RequestMapping(value="/getAllEnv",method={RequestMethod.POST})
	@ResponseBody
	public Result getAllEnv(){
		 Map<String, Object> map = new HashMap<String, Object>();
		List<Env> envList = null;
		try {
			envList = envUserService.getAllEnv();
			//取得用户的角色为环境管理员所在的项目
			//Integer envId = 
			map.put("envList", envList);
			return new Result(true, envList, "查询完毕");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("查询环境失败");
			return new Result(false, "查询环境失败");
		}
	}
	
	
}